
#include <BGridLayout>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BLabel>
#include <BPainter>

#include "LineStripePanel.h"

LineStripePanel::LineStripePanel(BWidget* parent) : BWidget(parent)
{
	this->setTitle("BLineStripe");
	_lineStripe = 0;

	BHBoxLayout* topLayout = new BHBoxLayout();
	{
		_closeLoopCheckBox = new BCheckBox("close-loop");
		_cornerVisibleCheckBox = new BCheckBox("corner-visible");

		topLayout->addWidget(_closeLoopCheckBox);
		topLayout->addWidget(_cornerVisibleCheckBox);
	}
	
	BGridLayout* gridLayout = new BGridLayout();
	{
		_pointCountSpinBox = new BLongSpinBox();
		_lengthSpinBox = new BDoubleSpinBox();
		_thicknessSpinBox = new BDoubleSpinBox();
		_radiusSpinBox = new BFloatSpinBox();

		gridLayout->addWidget(0, 0, new BLabel("Point Count : "));
		gridLayout->addWidget(0, 1, _pointCountSpinBox);
		gridLayout->addWidget(1, 0, new BLabel("Length : "));
		gridLayout->addWidget(1, 1, _lengthSpinBox);
		gridLayout->addWidget(2, 0, new BLabel("Thickness:"));
		gridLayout->addWidget(2, 1, _thicknessSpinBox);
		gridLayout->addWidget(3, 0, new BLabel("Corner radius:"));
		gridLayout->addWidget(3, 1, _radiusSpinBox);
	}

	_radiusesListWidget = new BListWidget();
	_radiusesListWidget->setPerch(Part_Left, 16);

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(topLayout);
	vlayout->addLayout(gridLayout);
	vlayout->addWidget(_radiusesListWidget);

	connect(this, Signal_AssetChanged, &LineStripePanel::slotAssetChanged);

	connect(_closeLoopCheckBox, Signal_Toggled, &LineStripePanel::slotCloseLoopToggled);
	connect(_cornerVisibleCheckBox, Signal_Toggled, &LineStripePanel::slotCornerVisibleToggled);
	connect(_thicknessSpinBox, Signal_EditFinished, &LineStripePanel::slotThicknessEditFinished);
	connect(_radiusSpinBox, Signal_EditFinished, &LineStripePanel::slotRadiusEditFinished);
	connect(_radiusesListWidget, Signal_Framed, &LineStripePanel::slotRadiusesListWidgetFramed);
}
LineStripePanel::~LineStripePanel()
{

}

void LineStripePanel::slotAssetChanged(BObject* object, const BValue& value)
{
	BLineStripe* lineStripe = value;
	if (_lineStripe != lineStripe)
	{
		if (_lineStripe)
		{
			_radiusesListWidget->clearItems();
		}
		_lineStripe = lineStripe;
		if (_lineStripe)
		{
			_lengthSpinBox->setValue(_lineStripe->length());
			_closeLoopCheckBox->setChecked(_lineStripe->closeLoop());
			_thicknessSpinBox->setValue((double)_lineStripe->thickness());
			_radiusSpinBox->setValue((float)_lineStripe->radius());

			if (const BVectorArray* vertices = _lineStripe->vertices())
			{
				_pointCountSpinBox->setValue(vertices->size());
				for (int i = 0; i < vertices->size(); i++)
				{
					_radiusesListWidget->addItem(_lineStripe->radius(i));
				}
			}
		}
	}
}

void LineStripePanel::slotCloseLoopToggled(BObject* object, const BValue& value)
{
	bool closeLoop = value;
	_lineStripe->setCloseLoop(closeLoop);
}
void LineStripePanel::slotCornerVisibleToggled(BObject* object, const BValue& value)
{
	bool cornerVisible = value;
	_lineStripe->setCornerVisible(cornerVisible);
}
void LineStripePanel::slotThicknessEditFinished(BObject* object, const BValue& value)
{
	double thickness = value;
	_lineStripe->setThickness(thickness);
}
void LineStripePanel::slotRadiusEditFinished(BObject* object, const BValue& value)
{
	float radius = value;
	_lineStripe->setRadius(radius);
}
void LineStripePanel::slotRadiusesListWidgetFramed(BListWidget* listWidget, const BValue& value)
{
	BPainter painter(listWidget);
	BRect leftRect = listWidget->rect(Part_Left);
	painter.setColor(Color_Text);
	for (int i = 0; i < listWidget->itemCount(); i++)
	{
		BListItem* item = listWidget->item(i);
		BRect textRect = item->rect();
		textRect.left() = leftRect.left();
		textRect.right() = leftRect.right();
		painter.drawText(textRect, i);
	}
}
